package com.byl.modules.sys.controller;

import com.byl.common.modules.controller.BaseController;
import com.byl.modules.sys.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/15/015.
 */
@RestController
@RequestMapping("/api/sys/dict")
public class DictController extends BaseController {

    @Autowired
    DictService dictService;

    @GetMapping("/list")
    public String queryDict(String pid) {
        List list = dictService.listDictChild(pid);
        return success("字典列表", list);
    }

    @GetMapping("/type")
    public String queryDictType(){
        List list=dictService.queryDictType();
        return success("字典类别",list);
    }

    @GetMapping("/checkcode")
    public String checkCode(String code) {
        int rs = dictService.checkCode(code);
        return success("检查编码是否存在！", rs);
    }

    @PostMapping("/save")
    public String saveDict(@RequestBody Map map) {
        int rs = dictService.saveDict(map);
        return success("保存字典结果", rs);
    }

    @PostMapping("/update")
    public String updateDict(@RequestBody Map map) {
        int rs = dictService.updateDict(map);
        if (rs > 0)
            return success("更新成功", "");
        return fail("更新失败");
    }

    @GetMapping("/delete")
    public String delDict(String id) {
        int rs = dictService.deleteDict(id);
        if (rs > 0)
            return success("删除字典", rs);
        return fail("删除失败");
    }


    @PostMapping("/status/chg")
    public String editStatus(@RequestBody Map map) {
        int rs = dictService.editStatus(map);
        if (rs > 0)
            return success("更新成功", "");
        return fail("更新失败");
    }

    @GetMapping("/child")
    public String queryDictByParentType(String types) {
        Map<String,List<Map>> map = dictService.queryDictByParentType(types);
        return success("字典子项", map);
    }

    @GetMapping("/item")
    public String queryChildDict(String type){
        List list=dictService.queryChildDict(type);
        return success("子项",list);
    }


}
